﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ObjectTransposeApi;

namespace ObjectTransposeApiSystemTest.TestFixture
{
    [TestFixture]
    public class ToIndexedListSimpleTestFixture
    {
        public class IndexStructure
        {
            public int Index { get; set; }
            public string Value { get; set; }
        }

        [Test]
        public void NullTest()
        {
            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(null, "");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 0);
        }

        [Test]
        public void NoIndexTest()
        {
            var source = new
            {
                Value = "test"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 0);
        }

        [Test]
        public void WrongIndexTest()
        {
            var source = new
            {
                Index= 0,
                Value = "test"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "WrongIndex");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 0);
        }

        [Test]
        public void FirstIndexTest()
        {
            var source = new
            {
                Index = 0,
                Value = "test"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 1);
            Assert.AreEqual(singleStructures[0].Value, source.Value);
        }

        [Test]
        public void SecondIndexTest()
        {
            var source = new
            {
                Index = 1,
                Value = "test"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 1);
            Assert.AreEqual(singleStructures[0].Value, source.Value);
        }

        [Test]
        public void TwoIndexTest()
        {
            var sourceOne = new
            {
                Index = 0,
                Value = "testone"
            };

            var sourceTwo = new
            {
                Index = 1,
                Value = "testtwo"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(
                new object[] { sourceOne, sourceTwo},
                "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 2);
            Assert.AreEqual(singleStructures[0].Value, sourceOne.Value);
            Assert.AreEqual(singleStructures[1].Value, sourceTwo.Value);
        }

        [Test]
        public void TwoDuplicateIndexTest()
        {
            var sourceOne = new
            {
                Index = 0,
                Value = "testone"
            };

            var sourceTwo = new
            {
                Index = 0,
                Value = "testtwo"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(
                new object[] { sourceOne, sourceTwo},
                "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 1);
            Assert.AreEqual(singleStructures[0].Value, sourceTwo.Value);
        }

        [Test]
        public void TypeWrongValueTest()
        {
            var source = new
            {
                Index = 0,
                Value = 123
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 1);
            Assert.IsNull(singleStructures[0].Value);
        }

        [Test]
        public void TypeWrongIndexTest()
        {
            var source = new
            {
                Index = "0",
                Value = "123"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 0);
        }

        [Test]
        public void NameWrongTest()
        {
            var source = new
            {
                Index = 0,
                ValueOne = "test"
            };

            List<IndexStructure> singleStructures = ObjectToIndexedList.Copy<IndexStructure>(source, "Index");

            // make sure the field was set
            Assert.AreEqual(singleStructures.Count, 1);
            Assert.IsNull(singleStructures[0].Value);
        }
    }
}
